# -*- encoding=utf8 -*-
__author__ = "86178"
'''
    Authour : tian
    usage : 可转债自动打新
    pre-condtion : 下载同花顺客户端并抽出同花顺东方版独立下单放置于桌面
    finish-time : 2021/1/2
    usage: "C:\Program Files\AirtestIDE\AirtestIDE\AirtestIDE" pyrunner "D:\projects\zqtz\autoNewDebt\auto.py"  替换为本地位置即可
'''
import datetime
import logging

today_ = datetime.datetime.today().strftime("%Y-%m-%d")

try:
    from airtest.core.api import *
    from airtest.cli.parser import cli_setup

    if not cli_setup():
        auto_setup(__file__, logdir=True, devices=[
            "Windows:///",
        ])

    import win32con
    import win32api
    import win32process

    # win+D  回到桌面
    win32api.keybd_event(0x5B, 0, 0, 0)
    win32api.keybd_event(0x44, 0, 0, 0)
    win32api.keybd_event(0x44, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(0x5B, 0, win32con.KEYEVENTF_KEYUP, 0)

    win32process.CreateProcess(r'D:\Program Files\weituo\东方证券\xiadan.exe', '',
                               None, None, 0, win32process.CREATE_NO_WINDOW, None, None,
                               win32process.STARTUPINFO())

    sleep(2.0)

    # 输入账户
    text("06807770")

    touch(Template(r"tpl1607843104229.png", record_pos=(-0.112, 0.018), resolution=(2560, 1440)))
    keyevent("199612")

    touch(Template(r"tpl1607843454938.png", record_pos=(-0.115, 0.043), resolution=(2560, 1440)))
    keyevent("199612")

    touch(Template(r"tpl1607843495143.png", record_pos=(-0.127, 0.07), resolution=(2560, 1440)))

    sleep(1)

    # debug point.1 识别率不高
    points = touch(Template(r"tpl1607843605549.png", record_pos=(-0.455, 0.247), resolution=(2560, 1440)))
    print(points)

    touch(Template(r"tpl1607844788688.png", record_pos=(-0.437, -0.177), resolution=(2560, 1440)))

    touch(Template(r"tpl1607843640071.png", record_pos=(-0.332, -0.207), resolution=(2560, 1440)))

    touch(Template(r"tpl1607843662368.png", record_pos=(-0.257, -0.207), resolution=(2560, 1440)))
    sleep(0.5)

    # 最终结束
    touch(Template(r"tpl1607843758180.png", record_pos=(-0.039, -0.252), resolution=(2560, 1440)))

except Exception as e:

    logname = '可转债打新%s.txt' % (today_)
    logging.basicConfig(filename=logname, level=logging.DEBUG)
    logging.error(e)
finally:
    from airtest.report.report import simple_report

    simple_report(__file__, logpath=True, output="可转债打新%s.html" % (today_))
